/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telaspessoa;


import java.util.Date;
import java.util.Set;
import javax.persistence.PersistenceException;
import javax.swing.JOptionPane;
import model.Email;
import model.Pessoa;
import cadastropessoadao.PessoaDao;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import model.Telefone;

/**
 *
 * @author Davi
 */
public class CadastroDePessoa extends javax.swing.JFrame {

    /**
     * Creates new form Tabela
     */
    public CadastroDePessoa() {
        initComponents();
        setLocationRelativeTo(null);
        setResizable(false);
        modTelefone=(DefaultTableModel) tebelaTelefone.getModel();
         modEmail=(DefaultTableModel) tabelaEmail.getModel();
        pessoadao = new PessoaDao();
        preencherJTable();///indo na banco e trazendo para tabela
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton4 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        bGravar = new javax.swing.JButton();
        bLimpar = new javax.swing.JButton();
        tNome = new javax.swing.JTextField();
        tCpf = new javax.swing.JTextField();
        tdatanacimento = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaEmail = new javax.swing.JTable();
        addTelefome = new javax.swing.JButton();
        addEmail = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tebelaTelefone = new javax.swing.JTable();

        jButton4.setText("jButton4");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setBackground(new java.awt.Color(153, 255, 0));
        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 2, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(230, 29, 29));
        jLabel2.setText("Cadastro de pessoa");

        jLabel3.setText("Nome");

        jLabel4.setText("CPF");

        jLabel5.setText("Dada de nacimento");

        bGravar.setText("Gravar");
        bGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGravarActionPerformed(evt);
            }
        });

        bLimpar.setText("Limpar");
        bLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLimparActionPerformed(evt);
            }
        });

        tCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tCpfActionPerformed(evt);
            }
        });

        tabelaEmail.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "email"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabelaEmail);

        addTelefome.setText("Adicionar Telefone");
        addTelefome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTelefomeActionPerformed(evt);
            }
        });

        addEmail.setText("Adicionar Email");
        addEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addEmailActionPerformed(evt);
            }
        });

        tebelaTelefone.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DDD", "Numero", "Tipo Telefone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tebelaTelefone);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(addTelefome)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addEmail)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(bGravar)
                        .addGap(55, 55, 55)
                        .addComponent(bLimpar))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(tNome, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tdatanacimento, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(0, 132, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(9, 9, 9)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tNome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tdatanacimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addComponent(addTelefome)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(addEmail)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bGravar)
                    .addComponent(bLimpar))
                .addContainerGap(96, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 3, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGravarActionPerformed
    String nome =tNome.getText();
    String cpf=tCpf.getText();
        SimpleDateFormat format=new SimpleDateFormat("dd-mm-yyyy");
       String dateString=tdatanacimento.getText();
        Date date=null;
        try {
           date=format.parse(dateString);
        } catch (Exception ex) {
            Logger.getLogger(CadastroDePessoa.class.getName()).log(Level.SEVERE,null,ex);
                    
        }
        System.out.println(format.format(date));
        pessoa=new Pessoa(nome, cpf, date, emails, telefones);
        pessoadao.gravarPessoa(pessoa, emails, telefones);
            
    }//GEN-LAST:event_bGravarActionPerformed

    private void bLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLimparActionPerformed
       // limparTela();        // TODO add your handling code here:
    }//GEN-LAST:event_bLimparActionPerformed

    private void addTelefomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTelefomeActionPerformed
jdialogtelefone= new JDialogTelefone(this,true); 
Object[]Linha =new Object[3];
jdialogtelefone.setVisible(true);
String ddd,tel,tipo;
ddd = jdialogtelefone.getDDD();
tel=jdialogtelefone.getTele();
 tipo= jdialogtelefone.getTipotel();
Linha[0]=jdialogtelefone.getDDD();
Linha[1]=tel;
Linha[2]=tipo;
telefone=new Telefone(null,ddd,tel,tipo);
telefones.add(telefone);
modTelefone.addRow(Linha);
jdialogtelefone.dispose();
    }//GEN-LAST:event_addTelefomeActionPerformed

    private void addEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addEmailActionPerformed
       jdialogEmail=new JDialogEmail(this,true);
       jdialogEmail.setVisible(true);
       Object[]Linha =new Object[1];
       Linha[0]=jdialogEmail.getEmail();
      modEmail.addRow(Linha);
      jdialogEmail.dispose();
      String descEmail=Linha[0].toString();
      email=new Email(descEmail,null);
      emails.add(email);
    
   
    
       
    }//GEN-LAST:event_addEmailActionPerformed

    private void tCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tCpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tCpfActionPerformed
 public static void main(String[] args) {
        new telaspessoa.CadastroDePessoa().setVisible(true);
 }
    public Runnable newAtualizarProdutosAction() {
        return new Runnable() {
            public void run() {
                try {
                    preencherJTable();
                } catch (PersistenceException ex) {
                    JOptionPane.showMessageDialog(null, "Erro ao consultar Mercadoria(s)");
               }
            }
        };
    }
    

  

    private void preencherJTable() {
  
              
            }
      //  } catch (Exception erro) {
        
   // }
    
    
    private DefaultTableModel modTelefone;
    private DefaultTableModel modEmail;
       private Email email;
  
    //lista dentro do listar tem cadastrar produto
    private Pessoa pessoa;
    private PessoaDao pessoadao;
    private Telefone telefone;
     private Set emails = new HashSet<Email>();
     private Set telefones = new HashSet<Telefone>();
     private JDialogTelefone jdialogtelefone;
     private JDialogEmail jdialogEmail;
    //objeto da clase produtoDao preenche esta tebala com dados
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addEmail;
    private javax.swing.JButton addTelefome;
    private javax.swing.JButton bGravar;
    private javax.swing.JButton bLimpar;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField tCpf;
    private javax.swing.JTextField tNome;
    private javax.swing.JTable tabelaEmail;
    private javax.swing.JTextField tdatanacimento;
    private javax.swing.JTable tebelaTelefone;
    // End of variables declaration//GEN-END:variables
}
